<?php
require_once '../classes/includeAll.php';
$title="Service Kontrolle";
$head ='<link rel="stylesheet" type="text/css" href="../design/css/controlService.css">';



if(!$isAdmin)
{
  header("Location: index.php");
}

require '../resources/getConnection.php';
$flagClass = new Flags($connection);
$flag = $flagClass->GetLastFlagOfType("ServiceStateChanged");
$serviceState = 3;
$state = 0;
$notice = "";
$search = array ("-", "ae",     "ue",     "oe");
$replace = array(" ", "&auml;", "&uuml;", "&ouml;");

if($flag !== false)
{
  foreach ((new allFlags())->GlobalFlags as $key => $value) {
    if(($flag & $value)>0)
    {
      $notice=str_replace($search, $replace, $key).'<br>';
    }
  }
  if(strpos($notice, "Fehler")!==false)
    $serviceState = -1;
  elseif(strpos($notice, "gestartet")!==false)
    $serviceState = 1;
  elseif(strpos($notice, "pausiert")!==false)
    $serviceState = 2;
  elseif(strpos($notice, "fortgesetzt")!==false)
    $serviceState = 1;
  elseif(strpos($notice, "gestoppt")!==false)
    $serviceState = 3;
}
$validServiceName=false;
if(isset($_POST["submit"]))
{
  $serviceName=  filter_input(INPUT_POST, "servicename");
  if(!($serviceName==null || $serviceName=="" || $serviceName==false))
  {
    $programClass = new Program($connection);
      $head.='<script src="../javascript/pagesJavascript/checkState.js" type="text/javascript"></script>';
      $_SESSION["flagID"] = -1;
      $_SESSION["flagIDGPF"] = -1;
      
      $validServiceName=true;
  switch ($_POST["submit"]) {
    case "Scan starten":
      $_SESSION["startDate"] =  ConvertDateTime(null,false);
      $_SESSION["SCPResult"]=$programClass->StartSCP("-s", $serviceName);
      header("Location: fullScan.php");
      break;
    case "Konfiguration laden":
      $_SESSION["startDate"] =  ConvertDateTime(null,false);
      $_SESSION["expectedResult"]=(new allFlags())->GlobalFlags["Konfiguration-upgedated"];
      $_SESSION["SCPResult"]=$programClass->StartSCP("-r", $serviceName);
      $state =1;
      break;
    case "Abfragen laden":
      $_SESSION["startDate"] =  ConvertDateTime(null,false);
      $_SESSION["expectedResult"]=(new allFlags())->GlobalFlags["Abfragen-upgedated"];
      $_SESSION["SCPResult"]=$programClass->StartSCP("-c", $serviceName);
      $state=2;
      break;
    case "Service Stoppen":
      $_SESSION["startDate"] =  ConvertDateTime(null,false);
      $_SESSION["expectedResult"]=(new allFlags())->GlobalFlags["Service-gestoppt"];
      $_SESSION["SCPResult"]=$programClass->StartSCP("-stop", $serviceName);
      $state=3;
      break;
    case "Service Pausieren":
      $_SESSION["startDate"] =  ConvertDateTime(null,false);
      $_SESSION["expectedResult"]=(new allFlags())->GlobalFlags["Service-pausiert"];
      $_SESSION["SCPResult"]=$programClass->StartSCP("-p", $serviceName);
      $state=4;
      break;
    case "Service Fortsetzten":
      $_SESSION["startDate"] =  ConvertDateTime(null,false);
      $_SESSION["expectedResult"]=(new allFlags())->GlobalFlags["Service-fortgesetzt"];
      $_SESSION["SCPResult"]=$programClass->StartSCP("-co", $serviceName);
      $state=5;
      break;
    case "Service Starten":
      $_SESSION["startDate"] =  ConvertDateTime(null,false);
      $allFlags=new allFlags();
      $_SESSION["expectedResult"]=$allFlags->GlobalFlags["Service-startet"] | $allFlags->GlobalFlags["Service-gestartet"];
      $_SESSION["SCPResult"]=$programClass->StartSCP("-start", $serviceName);
      $state=6;
      break;
  }
  }
}
$connection->disconnectDB();
require '../menu/head.php';
?>
<div id="controlArea">
  <h1>Service Kontrolle</h1>
  <?php if($serviceState == -1) {echo '<p class="error">Es ist ein Fehler aufgetreten</p>';} else {echo "<p>$notice</p>";} ?>

  <form action="controlService.php" method="POST">
    Service Name: <input type="text" name="servicename" value="<?php if(!$validServiceName) {echo 'InvIT Service';}else{echo $serviceName;}?>" id="serviceName"/>
  <table id="btnTable">
    
    <tbody>
      <tr>
        <td><input type="submit" value="Scan starten" name="submit" <?php if($serviceState != 1) {echo 'disabled="disabled"';}?> class="controlBtn" /></td>
        <td><input type="submit" value="Service Starten" name="submit" <?php if($serviceState != 3) {echo 'disabled="disabled"';}?> class="controlBtn"  />&nbsp;<?php if($state==6){echo'<span id="progresspic"><img src="../design/pictures/loading.gif" width="16px" alt="Bitte warten..."/>&nbsp;</span>';}?></td>
      </tr>
      <tr>
        <td><input type="submit" value="Konfiguration laden" name="submit" <?php if($serviceState != 1) {echo 'disabled="disabled"';}?> class="controlBtn"  />&nbsp;<?php if($state==1){echo'<span id="progresspic"><img src="../design/pictures/loading.gif" width="16px" alt="Bitte warten..."/>&nbsp;</span>';}?></td>
        <td><input type="submit" value="Service Stoppen" name="submit" <?php if($serviceState != 1 && $serviceState != 2) {echo 'disabled="disabled"';}?> class="controlBtn"  />&nbsp;<?php if($state==3){echo'<span id="progresspic"><img src="../design/pictures/loading.gif" width="16px" alt="Bitte warten..."/>&nbsp;</span>';}?></td>
      </tr>
      <tr>
        <td><input type="submit" value="Abfragen laden" name="submit" <?php if($serviceState != 1) {echo 'disabled="disabled"';}?> class="controlBtn"  />&nbsp;<?php if($state==2){echo'<span id="progresspic"><img src="../design/pictures/loading.gif" width="16px" alt="Bitte warten..."/>&nbsp;</span>';}?></td>
        <td><input type="submit" value="Service Pausieren" name="submit" <?php if($serviceState != 1) {echo 'disabled="disabled"';}?> class="controlBtn"  />&nbsp;<?php if($state==4){echo'<span id="progresspic"><img src="../design/pictures/loading.gif" width="16px" alt="Bitte warten..."/>&nbsp;</span>';}?></td>
      </tr>
      <tr>
        <td></td>
        <td><input type="submit" value="Service Fortsetzten" name="submit" <?php if($serviceState != 2) {echo 'disabled="disabled"';}?> class="controlBtn"  />&nbsp;<?php if($state==5){echo'<span id="progresspic"><img src="../design/pictures/loading.gif" width="16px" alt="Bitte warten..."/>&nbsp;</span>';}?></td>
      </tr>
    </tbody>
  </table>

</form>
</div>

<?php 
  require '../menu/footer.php';
?>